<%--
  Created by IntelliJ IDEA.
  User: xiaox
  Date: 2024/12/28
  Time: 16:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <style>
        /* 整体页面布局，使内容垂直水平居中 */
        body {
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 20px;
            background-color: #b9def0;
        }

        /* 整体表单容器的样式 */
        .form-div {
            background-color: #fff;
            border: 1px solid #ccc;
            border-radius: 5px;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
            padding: 20px;
            width: 300px;
            /*margin: 0 auto;*/
        }

        /* 注册内容区域的样式 */
        .reg-content {
            margin-bottom: 20px;
        }

        /* 标题样式 */
        .reg-content h1 {
            color: #333;
        }

        /* 链接样式 */
        .reg-content a {
            color: #007BFF;
            text-decoration: none;
            margin-left: 5px;
        }

        /* 表单标签样式 */
        form label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        /* 输入框样式 */
        form input[type="text"],
        form input[type="newpassword"],
        form input[type="tel"]{
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            margin-bottom: 10px;
        }

        form input[type="num"]{
            width: 60%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 3px;
            margin-bottom: 10px;
        }

        .num-container {
            display: flex;
            align-items: center;
        }

        .num-container img {
            height: 32px; /* 根据输入框的高度调整 */
            width: 40%;
            margin-left: 10px; /* 图片与输入框之间的间距 */
            margin-bottom: 10px;

        }

        /* 错误消息样式 */
        .err_msg {
            color: red;
            font-size: 12px;
        }

        /* 使登录按钮与用户名输入框等宽 */
        form input[type="submit"] {
            width: 100%;
            background-color: #007BFF;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }

        .tel-container {
            display: flex;
            align-items: center;
        }

        .tel-container input[type = "yzm"] {
            width: 25%;
            padding: 10px;
            left: 5px;
            border: 1px solid #ccc;
            border-radius: 3px;
            background-color: #007BFF;
            color: white;
            cursor: pointer;
            margin-bottom: 10px;
        }

    </style>
</head>
<body>
<div class="form-div">
    <div class="reg-content" style="text-align: center">
        <h1>忘记密码</h1>
    </div>
    <form action="forgot-password" method="post" id="reg-form">
        <label for="username">用户名：</label>
        <input type="text" id="username" name="username" placeholder="请输入用户名" required>
        <br>
        <span id="username_err" class="err_msg" style="display:none">用户名有误</span>

        <label for="newpassword">密码：</label>
        <input type="newpassword" id="newpassword" name="newpassword" placeholder="请输入新密码" required>
        <br>
        <span id="newpassword_err" class="err_msg" style="display: none">密码由长度6~12 单词字符组成</span>


        <input type="newpassword" id="newpassword1" name="newpassword"  placeholder="请再次输入密码" required>
        <br>
        <span id="newpassword1_err" class="err_msg" style="display: none">密码与第一次不同</span>


        <label for="tel">手机号：</label>
        <div class="tel-container">
        <input type="tel" id="tel" name="tel"  placeholder="请输入手机号" required>
        </div>
        <br>
        <span id="tel_err" class="err_msg" style="display: none">手机号码格式有误</span>

        <label for="num">验证码：</label>
        <div class="num-container">
            <input type="num" id="num" name="num" placeholder="请输入验证码" required>
            <img src="17bfcb7ca82f9c6d95cd57a3d2739ec4.png">
        </div>
        <br>
        <span id="num_err" class="err_msg" style="display: none">验证码有误</span>

        <input type="submit" value="提交验证" onclick="submitNewPassword()">
    </form>
</div>

</body>
<script>
    //验证用户名
    var usernameInput = document.getElementById("username");
    usernameInput.onblur = checkUser;
    function checkUser(){
        var username = usernameInput.value.trim();
        //判断用户名是否符合规则：长度1~12 单词字符组成
        //正则表达式
        var reg = /^\w{1,12}$/;
        var flag= reg.test(username);
        if(flag){
            //符合规则
            document.getElementById("username_err").style.display = "none";
        }else{
            //不符合规则，将span标签显示出来，给出用户提示。
            document.getElementById("username_err").style.display = "";
        }
        return flag;
    }

    //验证密码
    var newpasswordInput = document.getElementById("newpassword");
    newpasswordInput.onblur = checkPassword;
    function checkPassword(){
        var password = passwordInput.value.trim();
        var reg = /^\w{6,12}$/;
        var flag= reg.test(newpassword);
        if(flag){
            document.getElementById("newpassword_err").style.display = "none";
        }else{
            document.getElementById("newpassword_err").style.display = "";
        }
        return flag;
    }

    //验证第二次密码与第一次密码是否相同
    var newpassword1Input = document.getElementById("newpassword1");
    newpassword1Input.onblur = checkPassword1;
    function checkPassword1() {
        var newpassword = document.getElementById("newpassword").value.trim();
        var newpassword1 = password1Input.value.trim();
        var flag = newpassword === newpassword1;
        if (flag) {
            document.getElementById("newpassword1_err").style.display = "none";
        } else {
            document.getElementById("newpassword1_err").style.display = "";
        }
        return flag;
    }

    //验证手机号
    const telInput = document.getElementById("phone");
    telInput.onblur = checkTel;
    function checkTel(){
        const phone = telInput.value.trim();
        const reg = /^[1]\d{10}$/;
        const flag = reg.test(phone);
        if(flag){
            document.getElementById("tel_err").style.display = "none";
        }else{
            document.getElementById("tel_err").style.display = "";
        }
        return flag;
    }

    // 验证验证码
    const numInput = document.getElementById('num');
    numInput.onblur = checknum;
    function checknum() {
        const num = numInput.value.trim();
        const reg = /^7364$/;
        const flag = reg.test(num);
        if (flag) {
            document.getElementById('num_err').style.display = "none";
        } else {
            document.getElementById('num_err').style.display = "";
        }
        return flag;
    }

    // // 获取新密码输入框的值
    // var newpassword = document.getElementById('newpassword').value;
    // // 获取用户名和手机号（假设已经有相应的获取逻辑）
    // var username = document.getElementById('username').value;
    // var tel = document.getElementById('tel').value;
    //
    // fetch('/forgot-password', {
    //     method: 'POST',
    //     headers: {
    //         'Content-Type': 'application/x-www-form-urlencoded'
    //     },
    //     body: 'username=' + username + '&tel=' + tel + '&newpassword=' + newpassword
    // })
    //     .then(response => response.text())
    //     .then(data => {
    //         console.log(data);
    //         // 根据返回结果进行相应的提示显示等操作
    //     });

    function submitNewPassword() {
        var username = document.getElementById('username').value;
        var tel = document.getElementById('tel').value;
        var newpassword = document.getElementById('newpassword').value;

        var xhttp = new XMLHttpRequest();
        xhttp.onreadystatechange = function () {
            if (this.readyState === 4 && this.status === 200) {
                console.log(this.responseText);
                // 根据服务器返回的结果进行提示等操作
            }
        };
        xhttp.open("POST", "/forgot-password", true);
        xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhttp.send("username=" + username + "&tel=" + tel + "&newpassword=" + newpassword);
    }



    //验证表单
    //获取表单对象
    var regForm = document.getElementById("reg-form");
    //绑定onsubmit事件
    regForm.onsubmit = function () {
        //挨个判断每一个表单项是否都符合要求，如果有一个不合符，则返回false
        var flag = checkUser() && checkPassword() && checkTel() && checknum();
        return flag;
    }


</script>
</html>
